﻿using Juli.Mes.Stoves.Dtos;
using Lion.AbpPro.Core;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;

namespace Juli.Mes.Stoves
{
    [Route("Stoves")]
    [Authorize]
    public class StoveAppService : MesAppService, IStoveAppService
    {
        private readonly IRepository<Stove> stoveRepository;
        private readonly IRepository<IdentityUser> userRepository;

        public StoveAppService(IRepository<Stove> stoveRepository,
             IRepository<IdentityUser> userRepository)
        {
            this.stoveRepository = stoveRepository;
            this.userRepository = userRepository;
        }

        [HttpPost("create")]
        [SwaggerOperation("新增炉", Tags = new[] { "Stoves" })]
        public async Task CreateStove(StoveInputDto input)
        {
            var entity    = ObjectMapper.Map<StoveInputDto, Stove>(input);
            await stoveRepository.InsertAsync(entity);
        }

        [HttpPost("delete")]
        [SwaggerOperation("删除炉", Tags = new[] { "Stoves" })]
        public async Task DeleteStove(IdInput input)
        {
            await stoveRepository.DeleteAsync(p => p.Id == input.Id);
        }

        [HttpPost("edit")]
        [SwaggerOperation("编辑炉", Tags = new[] { "Stoves" })]
        public async Task EditStove(StoveInputDto input)
        {
            var entity = await stoveRepository.GetAsync(p => p.Id == input.Id);
            ObjectMapper.Map(input, entity);
            await stoveRepository.UpdateAsync(entity);
        }

        [HttpPost("page")]
        [SwaggerOperation("分页查询炉", Tags = new[] { "Stoves" })]
        public async Task<PagedResultDto<StoveOutputDto>> ListStove(PageStoveInputDto input)
        {
            var stoveQuery = await stoveRepository.GetQueryableAsync();
            var userQuery =await userRepository.GetQueryableAsync();

            var query = from s in stoveQuery
                        join u1 in userQuery on s.CreatorId equals u1.Id
                        join u2 in userQuery on s.LastModifierId equals u2.Id into u22
                        from u23 in u22.DefaultIfEmpty()
                        select new StoveOutputDto()
                        {
                            Id = s.Id,
                            StoveCode=s.StoveCode,
                            StoveName=s.StoveName,
                            Description=s.Description,
                            Rfid=s.Rfid,
                            CreationTime=s.CreationTime,
                            LastModificationTime=s.LastModificationTime,
                            CreatorName =u1.Name,
                            LastModifierName=u23.Name
                        };

            query = query.WhereIf(!string.IsNullOrEmpty(input.Filter),
                p => p.StoveCode.Contains(input.Filter)
                || p.StoveName.Contains(input.Filter)
                || p.Rfid.Contains(input.Filter));

            var items = query
                .OrderBy(e => e.CreationTime)
                .PageBy(input.SkipCount, input.PageSize)
                .ToList();

            var count = query.Count();

            return new PagedResultDto<StoveOutputDto>()
            {
                Items = items,
                TotalCount = count,
            };
        }
    }
}
